﻿/*****************************************************************************
 
 Use of Ext is under the Ext JS license, for more information, 
 please visit http://extjs.com/products/license.php page. 
  
 You should confirm that you have read, understood and accept ethical criteria listed. 
 If not, see <http://www.mvcnext.com/license/>.
  
 Version: 1.0.0.0
 Website: http://www.mvcnext.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Reflection;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace MVCnExt.Web.UI.Ext
{
    /// <summary>
    /// A TreeLoader provides for lazy loading of an Ext.tree.TreeNode's child nodes from a specified URL. 
    /// The response must be a javascript Array definition who's elements are node definition objects
    /// </summary>
    [Description("A TreeLoader provides for lazy loading of an Ext.tree.TreeNode's child nodes from a specified URL. The response must be a javascript Array definition who's elements are node definition objects")]
    public class ExtTreeLoader : ExtConfig
    {
        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            if (list.Count == 0)
                list.Add(new ExtScriptControlDescriptor("Ext.tree.TreeNode", "ctl00"));

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            desc.Type = "Ext.tree.TreeLoader";

            String[] properties = new String[]{
                "clearOnLoad", "dataUrl", "preloadChildren", "timeout"       
            };

            Type component = typeof(ExtTreeLoader);

            foreach (String property in properties)
            {
                PropertyInfo i = component.GetProperty(property);

                if (i == null)
                    throw new InvalidOperationException(String.Format("Property not found, 'ExtTreeLoader.{0}'", property));

                Object value = i.GetValue(this, null);

                if (value == null)
                {
                    continue;
                }

                if (!(value is String))
                    desc.AddPropertyBase(property, value);
                else
                    desc.AddPropertyBase(property, String.Format("'{0}'", value));
            }

            return list;
        }

        #region Public Properties

        [DefaultValue(true)]
        [Description("(optional) Default to true. Remove previously existing child nodes before loading.")]
        public Boolean? clearOnLoad { get; set; }

        [DefaultValue("")]
        [Description("The URL from which to request a Json string which specifies an array of node definition objects representing the child nodes to be loaded.")]
        public String dataUrl { get; set; }

        [DefaultValue(false)]
        [Description("If set to true, the loader recursively loads 'children' attributes when doing the first load on nodes.")]
        public Boolean? preloadChildren { get; set; }

        [DefaultValue(30000)]
        [Description("The timeout in milliseconds to be used for requests. (defaults to 30000)")]
        public String timeout { get; set; }

        #endregion
    }
}
